import java.util.ArrayList;
import java.util.List;

public class BM24 {

    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public int[] inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        inorderHelper(root, result);
        // 将 List 转换为 int 数组
        return result.stream().mapToInt(i -> i).toArray();
    }

    private void inorderHelper(TreeNode root, List<Integer> result) {
        if (root == null) {
            return;
        }
        inorderHelper(root.left, result);  // 递归遍历左子树
        result.add(root.val);              // 访问根节点
        inorderHelper(root.right, result); // 递归遍历右子树
    }
}
